<!--
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-02-20 19:27:43
 * @Description  : 试卷添加或者修改面板
-->

<template>
  <div class="bg-lightColor paper-edit-con p-3 text-[13px]">
    <n-grid x-gap="12" :cols="1" class="mb-2">
      <n-gi>
        <n-button
          type="primary"
          size="small"
          class="!text-[13px]"
          @click="backView"
        >
          <div class="mr-2 rotate-90 transform">
            <SvgIcon icon="xiala" color="#fff" size="10px"></SvgIcon>
          </div>
          返回
        </n-button>
      </n-gi>
    </n-grid>
    <n-divider class="!my-2" />
    <n-form
      ref="paperFormRef"
      :label-width="isSmall ? 100 : 120"
      label-placement="left"
      :label-align="isSmall ? 'left' : 'right'"
      :model="paper"
      size="small"
      require-mark-placement="right"
      :rules="formRule"
    >
      <n-form-item label="试卷名称：" path="name">
        <n-input
          v-model:value="paper.name"
          :allow-input="noSideSpace"
          placeholder="请输入试卷名称"
          clearable
          class="!w-[300px]"
          :maxlength="50"
        />
      </n-form-item>
      <n-form-item label="所属章节：" path="chapterId">
        <!-- <ChapterSelect
          :multiple="false"
          :default-chapter-id="paper.chapterId"
          @change-value="(value: any) => (paper.chapterId = value)"
        ></ChapterSelect> -->
        <CommonSelect
          :has-paper="false"
          :has-btn="false"
          @change-value="(value: any) => (paper.chapterId = value.chapterId)"
        ></CommonSelect>
      </n-form-item>
      <n-form-item label="人数：" path="people">
        <n-input
          v-model:value="paper.people"
          placeholder="请输入人数："
          :allow-input="noSideSpace"
          clearable
          class="!w-[300px]"
          :maxlength="20"
        />
      </n-form-item>
      <n-form-item label="时间限制：" path="time">
        <n-input
          v-model:value="paper.time"
          :allow-input="noSideSpace"
          placeholder="请输入时间限制"
          clearable
          class="!w-[300px]"
          :maxlength="50"
        />
      </n-form-item>
      <n-form-item label="图片：" path="pic">
        <UploadPanel
          v-if="!isModify || (isInit && isModify)"
          :file-list="previewFileList"
          @file-change="handlePicChange"
        ></UploadPanel>
      </n-form-item>
      <n-form-item label="paper文件：" path="url">
        <UploadPanel
          v-if="(!isModify || (isInit && isModify)) && rendera"
          :file-list="paperFileList"
          @file-change="handleUrlChange"
        ></UploadPanel>
      </n-form-item>
      <n-form-item label=" " v-permission="['admin']">
        <n-button
          type="primary"
          class="grent-btn w-[120px]"
          @click="handleSave(false)"
        >
          保存
        </n-button>
        <n-button
          v-if="!isModify"
          type="primary"
          class="grent-btn w-[120px] ml-2"
          @click="handleSave(true)"
        >
          保存并新增
        </n-button>
      </n-form-item>
    </n-form>
  </div>
</template>

<script setup lang="ts">
import type { FormInst, UploadFileInfo } from 'naive-ui'
import type { PaperRes, PaperUpdateReq } from '@/types/paper'
import { noSideSpace } from '@/utils'
import { userMenuShow } from '@/hooks/useMenuShow'
import UploadPanel from '@/views/User/Explorer/components/UploadPanel.vue'
const { isSmall } = userMenuShow()

const props = defineProps<{
  isModify: boolean
  paperRes?: PaperRes
}>()

const rendera = ref(true)

const emits = defineEmits<{
  (e: 'backView'): void
  (e: 'changePaper', type: number, data: PaperUpdateReq, addNew: boolean): void
}>()
const backView = () => {
  emits('backView')
}

const previewFileList = ref<UploadFileInfo[]>([])
const paperFileList = ref<UploadFileInfo[]>([])
const handlePicChange = (fileList: UploadFileInfo[]) => {
  if (fileList.length) {
    paper.pic = fileList[0].url as string
  }
}

const handleUrlChange = (fileList: UploadFileInfo[]) => {
  if (fileList.length) {
    paper.url = fileList[0].url as string
  }
}

const paper = reactive<PaperUpdateReq>({
  name: '',
  people: '',
  pic: '',
  chapterId: '',
  time: '',
  url: ''
})

const isInit = ref(false)
const initPaper = () => {
  if (props.isModify) {
    const { name, people, time, pic, id, chapterId } =
      props.paperRes as PaperRes
    paper.paperId = id
    paper.name = name
    paper.people = people
    paper.time = time
    paper.pic = pic
    paper.chapterId = chapterId
    if (paper.pic) {
      previewFileList.value = [
        {
          id: '1',
          status: 'finished',
          percentage: 100,
          url: paper.pic,
          name: paper.name
        }
      ]
    }
    isInit.value = true
  }
}

onMounted(() => {
  initPaper()
})

const formRule = {
  name: [
    {
      required: true,
      message: '请输入试卷名称',
      trigger: ['blur']
    },
    {
      min: 1,
      max: 50,
      trigger: ['blur'],
      message: '长度在1-50位'
    }
  ],
  chapterId: [
    {
      required: true,
      message: '请选择所属章节',
      trigger: ['blur']
    }
  ]
}

const paperFormRef = ref<FormInst | null>(null)

const handleSave = (addNew = false) => {
  paperFormRef.value?.validate((error: any) => {
    if (error) {
      return
    }
    if (props.isModify) {
      emits('changePaper', 1, paper, addNew)
    } else {
      emits('changePaper', 0, paper, addNew)
    }
    if (addNew) {
      // paper.name = ''
      paper.pic = ''
      paper.url = ''
      previewFileList.value = []
      paperFileList.value = []
      rendera.value = false
      setTimeout(() => {
        rendera.value = true
      }, 1000)
    }
  })
}
</script>

<style scoped lang="scss"></style>
